package com.sise.bl.controller;

import com.sise.bl.entity.Address;
import com.sise.bl.entity.User;
import com.sise.bl.service.AddressService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/address")
@SessionAttributes({"msg","loginUser"})
public class AddressController {
    @Autowired
    @Qualifier("addressService")
    private AddressService addressService;

    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public ModelAndView show(ModelAndView modelAndView, Model model) {
        User user = (User) model.getAttribute("loginUser");
        if (user == null) {
            modelAndView.addObject("msg", "需要先登录！");
            modelAndView.setViewName("login");
            return modelAndView;
        }

        int uid = user.getUid();

        List<Address> addresses = addressService.findAddressByUid(uid);

        modelAndView.addObject("list", addresses);
        modelAndView.setViewName("self_info");
        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestParam Map<String, String[]> map,Model model) throws InvocationTargetException, IllegalAccessException {

        //1.获取请求参数
        Address address = new Address();
        BeanUtils.populate(address, map);
        User user = (User) model.getAttribute("loginUser");
        user.getAddresses().add(address);
        address.setUser(user);

        //2.调用业务逻辑进行地址添加
        addressService.saveAddress(address);
        //3。转发到展示的方法
        return "redirect:/address/show";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(@RequestParam("aid") String aid) {
        //调用业务逻辑进行地址添加
        addressService.deleteAddressByAid(aid);
        //转发到展示的方法
        return "redirect:/address/show";
    }

    @RequestMapping(value = "/setDefault")
    public ModelAndView setDefault(@RequestParam("aid") String aid, ModelAndView modelAndView,Model model) {

        //1.获取请求参数
        User user = (User) model.getAttribute("loginUser");
        if (user == null) {
            modelAndView.addObject("msg", "需要先登录！");
            modelAndView.setViewName("login");
            return modelAndView;
        }

        //2.调用业务逻辑进行地址添加
        addressService.setAddressToDefault(aid, user.getUid());
        //3。转发到展示的方法
        modelAndView.setViewName("self_info");
        return modelAndView;
    }

    @RequestMapping(value = "/update")
    public String update(@RequestParam Map<String, String[]> map) throws InvocationTargetException, IllegalAccessException {

        //1.获取请求参数
        Address address = new Address();
        BeanUtils.populate(address, map);

        //2.调用业务逻辑进行地址添加
        addressService.updateByAid(address);
        //3。转发到展示的方法
        return "redirect:/address/show";
    }

}
